package com.hzit.servlet;

import com.alibaba.fastjson.JSON;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;
import com.hzit.vo.StudentVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	//1. 定义StudentService
	private StudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 指定响应对象的内容类型
		resp.setContentType("text/html;charset=utf8");
		//2. 查询出所有学生列表
		List<StudentVo> students = studentService.findAll();
		//3. 转换为json串
		String s = JSON.toJSONString(students);
		//4. 输出
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}
}
